package com.ipzoe.mem.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.google.common.collect.Lists;
import com.ipzoe.mem.domain.Dealer;
import com.ipzoe.mem.domain.MemberRechargePackage;
import com.ipzoe.mem.model.MemberCardAddBatchDTO;
import com.ipzoe.mem.service.IDealerService;
import com.ipzoe.mem.service.IMemberRechargePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ipzoe.mem.mapper.MemberCardMapper;
import com.ipzoe.mem.domain.MemberCard;
import com.ipzoe.mem.service.IMemberCardService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会员卡Service业务层处理
 *
 * @author eric
 * @date 2020-08-20
 */
@Service
public class MemberCardServiceImpl extends ServiceImpl<MemberCardMapper, MemberCard> implements IMemberCardService {

    @Autowired
    private IMemberRechargePackageService iMemberRechargePackageService;
    @Autowired
    private IDealerService iDealerService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void generateBatch(MemberCardAddBatchDTO memberCardAddBatchDTO) {

        MemberRechargePackage memberRechargePackage = iMemberRechargePackageService.getById(memberCardAddBatchDTO.getPackageId());
        Dealer dealer = iDealerService.getById(memberCardAddBatchDTO.getDealerId());

        List<MemberCard> memberCards = Lists.newArrayList();
        for (int i = 0; i < memberCardAddBatchDTO.getNum(); i++) {
            memberCards.add(new MemberCard()
                    .setCardNum(generateCardNum())
            .setCreateBy(memberCardAddBatchDTO.getCreateBy())
            .setDealerId(memberCardAddBatchDTO.getDealerId())
            .setExpiryDay(memberCardAddBatchDTO.getExpiryDay())
            .setPackageId(memberCardAddBatchDTO.getPackageId())
            .setStatus(0));
        }

        if(CollUtil.isNotEmpty(memberCards)){
            saveBatch(memberCards, 1000);
        }

    }

    @Override
    public String generateCardNum() {
        return IdUtil.objectId();
    }
}
